package cn.test.my.excel.poi;


import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 多个sheet excel 导出
 *
 * @author summit
 * @since 2021/6/1 14:19
 */

@SuppressWarnings("unused")
public class PoiExcelMultiExport {

	private static final Logger log = LoggerFactory.getLogger(PoiExcelMultiExport.class);


	/**
	 * 声明一个工作薄
	 */
	private Workbook workbook;

	/**
	 * 输出
	 */
	private OutputStream out;

	private Map<Integer, PoiSheetExport<T>> sheetMap;

	private Integer count;

	private PoiExcelType poiExcelType;

	public PoiExcelMultiExport(String title, OutputStream out) {
		this(title, out, PoiExcelType.XLSX);
	}

	public PoiExcelMultiExport(String title, OutputStream out, PoiExcelType poiExcelType) {
		sheetMap = new HashMap<>(8);
		switch (poiExcelType) {
			case XLS:
				this.workbook = new HSSFWorkbook();
				break;
			case XLSX_1:
				this.workbook = new SXSSFWorkbook();
				break;
			default:
				this.workbook = new XSSFWorkbook();
		}
		this.poiExcelType = poiExcelType;
		this.out = out;
		this.count = -1;
	}


	public void beginExport(Class<?> clazz, String sheetTitle) {
		this.count++;
		PoiSheetExport<T> pse = new PoiSheetExport<>(workbook, out, clazz, count, sheetTitle, poiExcelType);
		pse.beginExport();
		sheetMap.put(count, pse);
	}

	public void beginExport(Class<?> clazz, String sheetTitle, Integer count) {
		PoiSheetExport<T> pse = new PoiSheetExport<>(workbook, out, clazz, count, sheetTitle, poiExcelType);
		pse.beginExport();
		sheetMap.put(count, pse);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void write(Collection items, Integer count) {
		PoiSheetExport<T> sheet = sheetMap.get(count);
		if (sheet != null) {
			sheet.write(items);
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void write(Collection items) {
		PoiSheetExport<T> sheet = sheetMap.get(count);
		if (sheet != null) {
			sheet.write(items);
		}
	}

	@SuppressWarnings("unused")
	public void endExport() {
		try {
			workbook.write(out);
		}
		catch (IOException e) {
			log.error("endExport ERROR", e);
			throw new RuntimeException("写入excel出错");
		}
	}

	public Workbook getWorkbook() {
		return workbook;
	}

	public void setWorkbook(HSSFWorkbook workbook) {
		this.workbook = workbook;
	}

	public OutputStream getOut() {
		return out;
	}

	public void setOut(OutputStream out) {
		this.out = out;
	}

	public Map<Integer, PoiSheetExport<T>> getSheetMap() {
		return sheetMap;
	}

	public void setSheetMap(Map<Integer, PoiSheetExport<T>> sheetMap) {
		this.sheetMap = sheetMap;
	}

	public Integer getCount() {
		return count;
	}

	public void setCount(Integer count) {
		this.count = count;
	}
}

